package com.nuanshui.heatedloan.msgcenter.service;

import java.util.Date;
import java.util.List;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.baseservice.facade.ImageService;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.model.Image;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuanshui.heatedloan.baseservice.service.CommonServiceImpl;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.msgcenter.dal.mapper.SystemMsgMapper;
import com.nuanshui.heatedloan.msgcenter.facade.SystemMsgService;
import com.nuanshui.heatedloan.msgcenter.model.SystemMsg;
import com.nuanshui.heatedloan.msgcenter.vo.SystemMsgVO;
import com.nuanshui.heatedloan.pageable.Pager;

@Service("systemMsgService")
@Transactional(readOnly=true,value=TransactionManagerType.MSGCENTER_TRANSACTION_MARGER)
public class SystemMsgServiceImpl extends CommonServiceImpl implements SystemMsgService{

	@Autowired
	private SystemMsgMapper systemMsgMapper;

	@Reference(url = GlobalParam.BASESERVICE_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.BASESERVICE_DUBBO_SERVER_VERSION)
	private ImageService imageService;

	@Override
	public Date findSysMsgLastUpdatedTime() {
		return systemMsgMapper.findSysMsgLastUpdatedTime();
	}

	@Override
	@Transactional(TransactionManagerType.MSGCENTER_TRANSACTION_MARGER)
	public void insert(SystemMsg systemMsg) {
		 systemMsgMapper.insert(systemMsg);
	}
	
	@Override
	public Pager<SystemMsgVO> findAllSystemMsg(Integer bizType,String userId,Pager<SystemMsgVO> pager) {
		List<SystemMsgVO> list =  systemMsgMapper.findAllSystemMsg(bizType,userId,pager);

		for (SystemMsgVO item : list) {
			handMsgItem(item);
		}
		pager.setData(list);
		return pager;
	}

	private void handMsgItem(SystemMsgVO item)
	{
		//设置图片
		if (SysStringUtils.isNotEmpty(item.getImageId())) {
			Image image = imageService.load(item.getImageId());
			item.setImage(image);
		}
	}

	@Override
	public SystemMsgVO loadSystemMsgById(String id) {
		SystemMsgVO item=systemMsgMapper.loadSystemMsgById(id);
		handMsgItem(item);
		return item;
	}
}
